//
//  XLGuardianListViewController.m
//  XiangLove
//
//  Created by 陈宪栋 on 2017/12/20.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLGuardianListViewController.h"
#import "XLPPGuardianTableViewCell.h"
#import "XLPPGuardianModel.h"
#import <MJRefresh.h>

@interface XLGuardianListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation XLGuardianListViewController
{
    NSInteger page;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    page = 1;
    
    //请求守护榜信息
    [self reqeustSource];
    
    self.navigationItem.title = @"守护榜";
    [self showBackButton:NULL];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.rowHeight = 72;
    _tableView.tableFooterView = [UIView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        
        //重新加载数据
        [self reqeustSource];
        
    }];
//    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        
//        //重新加载数据
//        [self reqeustSource];
//        
//    }];
    
    [_tableView registerClass:[XLPPGuardianTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XLPPGuardianTableViewCell class])];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

//请求信息
- (void)reqeustSource
{
//    7492867
    [[[XLHTTPSessionManager sharedManager] XLV2GET:@"/api/v2/userInfo/GetknightList" parameters:@{@"userId":self.userId,@"pageIndex":@(1),@"pageSize":@"20"} resultClass:nil] subscribeNext:^(id x) {
        
        //如果是上拉刷新 重置所有数据
        if (page == 1) {
            
            if (self.dataArray.count > 0) {
                [self.dataArray removeAllObjects];
            }
            
        }
            
        [self.dataArray addObjectsFromArray:x];
        
        if (self.dataArray.count > 0) {
            page++;
        }
        
        if (self.dataArray.count < page *10) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"数据请求失败 XLGuardianListViewController");
         [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XLPPGuardianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XLPPGuardianTableViewCell class]) forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    
    XLPPGuardianModel *model = [[XLPPGuardianModel alloc] init];
    
    if (dic) {
        model.HeadImg = [NSString stringWithFormat:@"%@",dic[@"Url"]];
        
        model.UserId = [NSString stringWithFormat:@"%@",dic[@"Id"]];
        
        model.NickName = [NSString stringWithFormat:@"%@",dic[@"Name"]];
        
        CGFloat money = [NSString stringWithFormat:@"%@",dic[@"Money"]].floatValue;
        
        model.AmountCount = [NSString stringWithFormat:@"%.lf",money*10];
    }
    
    
    
    cell.rankLabel.text = F(@"%zd", indexPath.row + 1);
    [cell.avatarImageView setImageWithURL:[NSURL URLWithString:model.HeadImg] placeholder:[UIImage imageNamed:@""]];
    cell.nickNameLabel.text = model.NickName;
    cell.goldBeanLabel.text = F(@"%.0f金豆", model.AmountCount.doubleValue);
    cell.rankLabel.hidden = indexPath.row < 3;
    cell.rankImageView.hidden = indexPath.row > 3;
    cell.goldBeanLabel.textColor = indexPath.row < 3 ? RGB_HEX(0xffbb01) : kBlackColor;
    if (indexPath.row == 0) {
        cell.rankImageView.image = [UIImage imageNamed:@"conten_pic_shouhu1"];
        cell.avatarImageView.layer.borderColor = RGB(253, 225, 161).CGColor;
    } else
    if (indexPath.row == 1) {
        cell.rankImageView.image = [UIImage imageNamed:@"conten_pic_shouhu2"];
        cell.avatarImageView.layer.borderColor = RGB(143, 238, 239).CGColor;
    } else
    if (indexPath.row == 2) {
        cell.rankImageView.image = [UIImage imageNamed:@"conten_pic_shouhu3"];
        cell.avatarImageView.layer.borderColor = RGB(239, 181, 136).CGColor;
    } else {
        cell.rankImageView.image = [UIImage imageNamed:@""];
        cell.avatarImageView.layer.borderColor = RGB(230, 230, 230).CGColor;
    }
    return cell;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        self.dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}

@end
